DROP FUNCTION IF EXISTS usp_[%tablename%]_sel_by_parent_[%parenttable%][%fks%]();

CREATE FUNCTION usp_[%tablename%]_sel_by_parent_[%parenttable%][%fks%] (
  [%additionalparams%]
  [%params%]
  
)   RETURNS refcursor AS $$
-- Select a set of [%tablename%] table records by  [%parenttable%][%fks%].
--  [%datestamp%] - auto generated
  DECLARE
      ref refcursor;  
      BEGIN 
   OPEN ref FOR SELECT
    A.* 
  FROM [%tablename%] A [%joins%]
  WHERE 
    [%whereitems%]
    REF_[%toptable%].[%fieldname%] = [%fieldname%]  AND 
    A.is_deleted = 0;
END;
    $$ LANGUAGE plpgsql;